package com.ruoyi.web.controller.laiqianba.account;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.account.service.IAccountInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.AccountInfo;
import com.ruoyi.laiqianba.enums.PostInfoEnum;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.wrapper.AccountInfoWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 总账户信息Controller
 * 
 * @author ruoyi
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/account/accountInfo")
public class AccountInfoController extends BaseController
{
    @Autowired
    private IAccountInfoService accountInfoService;
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/extData")
    public AjaxResult extData()
    {
        JSONObject returnData = new JSONObject();
        // 账户类型
        // 根据用户的
        returnData.put("ownerInfoList", sysUserService.getUserInfoByPostId(PostInfoEnum.ACCOUNT_MANAGER.getPostId()));

        return AjaxResult.success(returnData);
    }

    /**
     * 查询总账户信息列表
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountInfo accountInfo)
    {
        startPage();
        List<Map<String, Object>> list = accountInfoService.selectAccountInfoMapList(accountInfo);
        list = new AccountInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出总账户信息列表
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:export')")
    @Log(title = "总账户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountInfo accountInfo)
    {
        List<AccountInfo> list = accountInfoService.selectAccountInfoList(accountInfo);
        ExcelUtil<AccountInfo> util = new ExcelUtil<AccountInfo>(AccountInfo.class);
        util.exportExcel(response, list, "总账户信息数据");
    }

    /**
     * 获取总账户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(accountInfoService.selectAccountInfoById(id));
    }

    /**
     * 新增总账户信息
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:add')")
    @Log(title = "总账户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccountInfo accountInfo)
    {
        SysUser sysUser = sysUserService.selectUserById(accountInfo.getOwnerId());
        accountInfo.setOwnerName(sysUser.getNickName());
        return toAjax(accountInfoService.insertAccountInfo(accountInfo));
    }

    /**
     * 修改总账户信息
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:edit')")
    @Log(title = "总账户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountInfo accountInfo)
    {
        SysUser sysUser = sysUserService.selectUserById(accountInfo.getOwnerId());
        accountInfo.setOwnerName(sysUser.getNickName());
        return toAjax(accountInfoService.updateAccountInfo(accountInfo));
    }

    /**
     * 删除总账户信息
     */
    @PreAuthorize("@ss.hasPermi('account:accountInfo:remove')")
    @Log(title = "总账户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(accountInfoService.deleteAccountInfoByIds(ids));
    }

}
